package org.jiuling.aliyun.sms.factory.service.impl;

import com.alibaba.fastjson2.JSON;
import com.aliyun.sdk.service.dysmsapi20170525.AsyncClient;
import com.aliyun.sdk.service.dysmsapi20170525.models.SendBatchSmsRequest;
import com.aliyun.sdk.service.dysmsapi20170525.models.SendSmsRequest;
import org.jiuling.aliyun.sms.config.AliYunSmsConfig;
import org.jiuling.aliyun.sms.convert.SendSmsResponseConvert;
import org.jiuling.aliyun.sms.exception.AliYunSMSRuntimeException;
import org.jiuling.aliyun.sms.factory.AliYunSmsAsynchronous;
import org.jiuling.aliyun.sms.factory.service.AliYunSendSmsService;
import org.jiuling.aliyun.sms.factory.service.verify.AliYunSendSmsVerify;
import org.jiuling.aliyun.sms.model.SendSmsResponseModel;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;

/**
 * 阿里云异步短信服务实现类
 * @author 紫灵
 * @date 2023年02月09日 17时45分36秒
 */
public class AliYunAsyncSendSmsServiceImpl extends AliYunSmsAsynchronous implements AliYunSendSmsService {


    public AliYunAsyncSendSmsServiceImpl(AliYunSmsConfig aliYunSmsConfig) {
        super(aliYunSmsConfig);
    }

    @Override
    public SendSmsResponseModel sendSms(String signName,String phoneNumber, String templateCode, Map<String, Object> templateParam) {
        // 校验发送短信参数
        AliYunSendSmsVerify.sendSmsParam(signName, phoneNumber, templateCode);
        // 构建发送短信请求
        SendSmsRequest sendSmsRequest = SendSmsRequest.builder()
                .phoneNumbers(phoneNumber)
                .signName(signName)
                .templateCode(templateCode)
                .templateParam(JSON.toJSONString(templateParam))
                .build();
        // 创建客户端
        try (AsyncClient asyncClient = create()){
            //异步获取API请求的返回值
            return SendSmsResponseConvert.bodyToModel(asyncClient.sendSms(sendSmsRequest).get().getBody());
        } catch (InterruptedException | ExecutionException e) {
            throw new AliYunSMSRuntimeException(e.getLocalizedMessage());
        }

    }

    @Override
    public SendSmsResponseModel sendBatchSms(List<String> signNameList, List<String> phoneNumberList, String templateCode,List<Map<String,Object>> templateParamList) {
        // 检验发送批量短信参数
        AliYunSendSmsVerify.sendBatchSmsParam(signNameList, phoneNumberList, templateCode, templateParamList);
        // 封装发送批量短信请求
        SendBatchSmsRequest sendBatchSmsRequest = SendBatchSmsRequest.builder()
                .phoneNumberJson(JSON.toJSONString(phoneNumberList))
                .signNameJson(JSON.toJSONString(signNameList))
                .templateCode(templateCode)
                .templateParamJson(JSON.toJSONString(templateParamList))
                .build();
        try(AsyncClient asyncClient = create()) {
            //异步获取API请求的返回值
            return SendSmsResponseConvert.bodyToModel(asyncClient.sendBatchSms(sendBatchSmsRequest).get().getBody());
        }catch (InterruptedException | ExecutionException e) {
            throw new AliYunSMSRuntimeException(e.getLocalizedMessage());
        }
    }


}
